package com.ddwl.order.dao;

import com.alibaba.fastjson.JSON;
import com.ddwl.common.constant.OrderEnum;
import com.ddwl.common.dao.TkDao;
import com.ddwl.common.exception.GlobalException;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.order.dao.mapper.OrderInfoMasterMapper;
import com.ddwl.order.dao.model.OrderInfoMaster;
import com.ddwl.schema.bo.order.OrderListApiBo;
import com.ddwl.schema.bo.order.QueryOrderListBo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.time.Instant;
import java.util.List;

/**
 * (OrderInfoMaster)表数据库访问层
 *
 * @author makejava
 * @since 2020-06-02 14:48:12
 */
@Slf4j
@Repository
public class OrderInfoMasterDao extends TkDao<OrderInfoMasterMapper, OrderInfoMaster> {

    /**
     * 查询订单列表包含商品信息
     * @return
     */
    public List<OrderInfoMaster> queryOrderSpuList(QueryOrderListBo bo){
        Example example = getExample();
        example.and().andEqualTo("status",bo.getStatus())
                .andIn("status",bo.getStatuss())
                .andEqualTo("userId",bo.getUserId())
                .andEqualTo("distUserId",bo.getDistUserId())
                .andEqualTo("regionUserId",bo.getRegionUserId())
                .andLike("id", MySqlUtil.rightLike(bo.getOrderNo()))
                .andEqualTo("payType",bo.getPayType())
                .andGreaterThanOrEqualTo("gmtCreated",bo.getStartTime())
                .andLessThan("gmtCreated",bo.getEndTime());
        List<OrderInfoMaster> OrderInfoMasters = baseMapper.selectByExample(example);
        return OrderInfoMasters;
    }


    /**
     * 查询订单列表
     * @return
     */
    public List<OrderInfoMaster> queryOrderSpuApiList(OrderListApiBo bo){
        Example example = getExample();
        example.and()
                .andEqualTo("status",bo.getStatus())
                .andNotEqualTo("status",0)
                .andEqualTo("id",bo.getOrderId())
                .andIn("supplierId",bo.getSupplierList())
        ;
        if (bo.getOrderByModify()!=null&&bo.getOrderByModify()){
            example.setOrderByClause("gmt_modified desc");
        }else {
            example.setOrderByClause("gmt_created desc");
        }
        return baseMapper.selectByExample(example);
    }



    /**
     * 关闭订单
     * @param orderNo
     */
    public void orderClosed(String orderNo,String userId,String msg){
        OrderInfoMaster OrderInfoMaster =  new OrderInfoMaster();
        OrderInfoMaster.setStatus(OrderEnum.OrderStatus.CLOSE.getCode());
        OrderInfoMaster.setGmtModified(Instant.now());
        Example example = new Example(OrderInfoMaster.class);
        example.and().andEqualTo("id",orderNo)
                .andEqualTo("userId",userId)
                .andEqualTo("status", OrderEnum.OrderStatus.UN_PAY.getCode());
        Integer i =baseMapper.updateByExampleSelective(OrderInfoMaster,example);
        if(i<1){
            log.error("关闭订单失败 orderNo={}", JSON.toJSONString(orderNo));
            throw new GlobalException("关闭订单失败");
        }
    }

    /**
     * 查询过期订单
     * @return
     */
    public List<OrderInfoMaster> selectExpirationOrder(){
        Example example = getExample();
        example.and()
                .andLessThan("expirationTime",Instant.now())
                .andEqualTo("status", OrderEnum.OrderStatus.UN_PAY.getCode());
        return baseMapper.selectByExample(example);
    }

    /**
     * 关闭超时的订单
     */
    public void orderExpirationClosed(){
        OrderInfoMaster OrderInfoMaster =  new OrderInfoMaster();
        OrderInfoMaster.setStatus(OrderEnum.OrderStatus.CLOSE.getCode());
        OrderInfoMaster.setGmtModified(Instant.now());
        Example example = new Example(OrderInfoMaster.class);
        example.and()
                .andLessThan("expirationTime",Instant.now())
                .andEqualTo("status", OrderEnum.OrderStatus.UN_PAY.getCode());
        baseMapper.updateByExampleSelective(OrderInfoMaster,example);
    }


}